<?php

class ReportAnalyzer
{
  public static function getReports($date = null, $name = null)
  {
    $c = new Criteria();
    
    if ($name) {
      $memid = is_numeric($name) ? $name : WrMemberPeer::getMemberIdByName($name); 
      $c->add(WrBasicPeer::MEMBER_ID, $memid);
    }
    if ($date) {
      $c->add(WrBasicPeer::DATE1, $date, Criteria::EQUAL);
      //$c->add(WrBasicPeer::DATE2, $date, Criteria::GREATER_EQUAL);
    }
    
    $c->addDescendingOrderByColumn(WrBasicPeer::DATE1);
    $reports = WrBasicPeer::doSelect($c);
    return $reports;
  }
  
  public static function getItems($cat, $day = null, $memid = null, $page = 0)
  {
    $c = new Criteria();
    if ($memid) {
      $c->add(WrBasicPeer::MEMBER_ID, $memid);
    }
    if ($day) {
      $c->add(WrBasicPeer::DATE1, $day, Criteria::EQUAL);
      //$c->add(WrBasicPeer::DATE2, $date, Criteria::GREATER_EQUAL);
    }
    
    $c->addSelectColumn(WrBasicPeer::ID);
    $stmt = WrBasicPeer::doSelectStmt($c);
    $IDs = array();
    while($res = $stmt->fetchColumn(0)) {
      $IDs[] = $res;
    }
    $conn = Propel::getConnection();
    $query = 'select count(*) as count from %s where %s="%s"';
    $query = sprintf($query, WrIssuePeer::TABLE_NAME, WrIssuePeer::CATEGORY, $cat);
    $stmt = $conn->prepare($query);
    $stmt->execute();
    $rs = $stmt->fetch(PDO::FETCH_OBJ);
    $is_issue = $rs->count;

    
    $c = new Criteria();
    $ret = null;
    if ($is_issue) {
      $ret['title'] = array('PR', 'Synopsis', 'Hour', 'Completed');
      $c->add(WrIssuePeer::BASIC_ID, $IDs, Criteria::IN);
      $c->add(WrIssuePeer::CATEGORY, $cat);
      $issues = WrIssuePeer::doSelect($c);
      foreach($issues as $issue) {
        $ret['data'][] = array($issue->getIssueId(), $issue->getSynopsis(), $issue->getHour(), $issue->getCompleted());
      }
    } else {
      $ret['title'] = array('Synopsis', 'Hour', 'Completed');
      $c->add(WrOthersPeer::BASIC_ID, $IDs, Criteria::IN);
      $c->add(WrOthersPeer::CATEGORY, $cat);
      $issues = WrOthersPeer::doSelect($c);
      foreach($issues as $issue) {
        $ret['data'][] = array($issue->getSynopsis(), $issue->getHour(), $issue->getCompleted());
      }
    }
    return $ret;
  }
  
  public static function getLoadDistribution($reports)
  {
    $retCount = null;
    $retHours = null;
    $ret = null;
    $names = null;
    $memid = null;
    if ($reports == null) return null;
    foreach($reports as $report)
    {
      $dist = $report->getCategoryDistribution();
      $results = $dist['distribution'];
      $names[] = $dist['name'];
      $memid[] = $dist['memid'];
      foreach ($results as $category => $result)
      {
        if (!isset($retHours[$category])) {
          $retCount[$category] = $result['count'];
          $retHours[$category] = $result['hours'];
        } else {
          $retCount[$category] += $result['count'];
          $retHours[$category] += $result['hours'];
        }
      }
    }
    sort($names, SORT_STRING);
    $ret = array(
      'name' => join(', ', $names), 'memid' => $memid,
      'date1' => $dist['date1'], 'date2' => $dist['date2'],
      'count' => $retCount, 'hours' => $retHours
    );
    return $ret;
  }
  
  public static function getDistSet($dist, $type, $url)
  {
    $memid = count($dist['memid']) == 1 ? $dist['memid'][0] : null;
    $link = $url.'?day='.$dist['date1'];
    if ($memid) $link .= '&memid='.$memid;
    $ret = array();
    foreach($dist[$type] as $cat => $value) {
      $ret[] = array('name' => $cat, 'value' => $value, 'link' => $link.'&category='.$cat);
    }
    return $ret;
  } 
}